{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "import ogr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "netCDF\n",
      "PCIDSK\n",
      "JP2OpenJPEG\n",
      "PDF\n",
      "DB2ODBC\n",
      "ESRI Shapefile\n",
      "MapInfo File\n",
      "UK .NTF\n",
      "OGR_SDTS\n",
      "S57\n",
      "DGN\n",
      "OGR_VRT\n",
      "REC\n",
      "Memory\n",
      "BNA\n",
      "CSV\n",
      "NAS\n",
      "GML\n",
      "GPX\n",
      "KML\n",
      "GeoJSON\n",
      "OGR_GMT\n",
      "GPKG\n",
      "SQLite\n",
      "ODBC\n",
      "WAsP\n",
      "PGeo\n",
      "MSSQLSpatial\n",
      "PostgreSQL\n",
      "OpenFileGDB\n",
      "XPlane\n",
      "DXF\n",
      "CAD\n",
      "Geoconcept\n",
      "GeoRSS\n",
      "GPSTrackMaker\n",
      "VFK\n",
      "PGDUMP\n",
      "OSM\n",
      "GPSBabel\n",
      "SUA\n",
      "OpenAir\n",
      "OGR_PDS\n",
      "WFS\n",
      "HTF\n",
      "AeronavFAA\n",
      "Geomedia\n",
      "EDIGEO\n",
      "GFT\n",
      "SVG\n",
      "CouchDB\n",
      "Cloudant\n",
      "Idrisi\n",
      "ARCGEN\n",
      "SEGUKOOA\n",
      "SEGY\n",
      "XLS\n",
      "ODS\n",
      "XLSX\n",
      "ElasticSearch\n",
      "Walk\n",
      "Carto\n",
      "AmigoCloud\n",
      "SXF\n",
      "Selafin\n",
      "JML\n",
      "PLSCENES\n",
      "CSW\n",
      "VDV\n",
      "GMLAS\n",
      "TIGER\n",
      "AVCBin\n",
      "AVCE00\n",
      "HTTP\n"
     ]
    }
   ],
   "source": [
    "cnt = ogr.GetDriverCount()\n",
    "for i in range(cnt):\n",
    "    driver = ogr.GetDriver(i)\n",
    "    driverName = driver.GetName()\n",
    "    print(driverName)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ARCGEN\n",
      "AVCBin\n",
      "AVCE00\n",
      "AeronavFAA\n",
      "AmigoCloud\n",
      "BNA\n",
      "CAD\n",
      "CSV\n",
      "CSW\n",
      "Carto\n",
      "Cloudant\n",
      "CouchDB\n",
      "DB2ODBC\n",
      "DGN\n",
      "DXF\n",
      "EDIGEO\n",
      "ESRI Shapefile\n",
      "ElasticSearch\n",
      "GFT\n",
      "GML\n",
      "GMLAS\n",
      "GPKG\n",
      "GPSBabel\n",
      "GPSTrackMaker\n",
      "GPX\n",
      "GeoJSON\n",
      "GeoRSS\n",
      "Geoconcept\n",
      "Geomedia\n",
      "HTF\n",
      "HTTP\n",
      "Idrisi\n",
      "JML\n",
      "JP2OpenJPEG\n",
      "KML\n",
      "MSSQLSpatial\n",
      "MapInfo File\n",
      "Memory\n",
      "NAS\n",
      "ODBC\n",
      "ODS\n",
      "OGR_GMT\n",
      "OGR_PDS\n",
      "OGR_SDTS\n",
      "OGR_VRT\n",
      "OSM\n",
      "OpenAir\n",
      "OpenFileGDB\n",
      "PCIDSK\n",
      "PDF\n",
      "PGDUMP\n",
      "PGeo\n",
      "PLSCENES\n",
      "PostgreSQL\n",
      "REC\n",
      "S57\n",
      "SEGUKOOA\n",
      "SEGY\n",
      "SQLite\n",
      "SUA\n",
      "SVG\n",
      "SXF\n",
      "Selafin\n",
      "TIGER\n",
      "UK .NTF\n",
      "VDV\n",
      "VFK\n",
      "WAsP\n",
      "WFS\n",
      "Walk\n",
      "XLS\n",
      "XLSX\n",
      "XPlane\n",
      "netCDF\n"
     ]
    }
   ],
   "source": [
    "drvName = []\n",
    "cnt = ogr.GetDriverCount()\n",
    "for i in range(cnt):\n",
    "    driver = ogr.GetDriver(i)\n",
    "    driverName = driver.GetName()\n",
    "    drvName.append(driverName)\n",
    "drvName.sort()\n",
    "for d in drvName:\n",
    "    print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "path = os.getcwd()+\"/shp/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\workspace\\\\DevWork\\\\PyExample\\\\exam4/shp/'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "pnt = path + \"北京_point.shp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = ogr.GetDriverByName('ESRI Shapefile')\n",
    "dataSource = driver.Open(pnt, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "layer = dataSource.GetLayerByIndex(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "图层描述 :北京_point\n",
      "图层范围 :(115.37294, 117.36857, 39.41652, 41.07743)\n",
      "要素数量 :128554\n",
      "元数据描述 :{'DBF_DATE_LAST_UPDATE': '2009-05-19'}\n",
      "空间参考 :GEOGCS[\"Geographic Coordinate System\",\n",
      "    DATUM[\"WGS84\",\n",
      "        SPHEROID[\"WGS84\",6378137.0,298.257223560493]],\n",
      "    PRIMEM[\"Greenwich\",0.0],\n",
      "    UNIT[\"degree\",0.0174532925199433],\n",
      "    AUTHORITY[\"EPSG\",\"4326\"]]\n"
     ]
    }
   ],
   "source": [
    "print(\"图层描述 :{0}\".format(layer.GetDescription()))\n",
    "print(\"图层范围 :{0}\".format(layer.GetExtent()))\n",
    "print(\"要素数量 :{0}\".format(layer.GetFeatureCount()))\n",
    "print(\"元数据描述 :{0}\".format(layer.GetMetadata()))\n",
    "print(\"空间参考 :{0}\".format(layer.GetSpatialRef()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字段名：NAME  字段类型：4  字段长度：String              字段精度：65\n",
      "字段名：LAYER  字段类型：4  字段长度：String              字段精度：21\n",
      "字段名：MARINE  字段类型：4  字段长度：String              字段精度：1\n",
      "字段名：RegionName  字段类型：4  字段长度：String              字段精度：6\n",
      "字段名：DataLevel  字段类型：0  字段长度：Integer              字段精度：1\n",
      "字段名：MP_TYPE  字段类型：4  字段长度：String              字段精度：6\n",
      "字段名：Phone  字段类型：4  字段长度：String              字段精度：12\n",
      "字段名：StreetDesc  字段类型：4  字段长度：String              字段精度：46\n",
      "字段名：HighwayIdx  字段类型：0  字段长度：Integer              字段精度：2\n",
      "字段名：ZipIdx  字段类型：0  字段长度：Integer              字段精度：1\n",
      "字段名：City  字段类型：4  字段长度：String              字段精度：1\n"
     ]
    }
   ],
   "source": [
    "layerDefinition = layer.GetLayerDefn()\n",
    "for i in range(layerDefinition.GetFieldCount()):\n",
    "    fieldName =  layerDefinition.GetFieldDefn(i).GetName()\n",
    "    fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType()\n",
    "    fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode)\n",
    "    fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()\n",
    "    GetPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()\n",
    "    print(\"字段名：{0}  字段类型：{1}  字段长度：{2}  \\\n",
    "            字段精度：{3}\".format(fieldName,fieldTypeCode,\n",
    "                            fieldType,fieldWidth,GetPrecision))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
